package com.itlnn.xfeng.commonview.canversview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.itlnn.xfeng.commonview.R;

public class TranslationView extends View {

    /**
     * 平移的矩阵
     */
    private Matrix matrix = new Matrix();

    private Bitmap bitmap ;

    private Paint paint ;


    public TranslationView(Context context) {
        this(context , null);
    }

    public TranslationView(Context context, AttributeSet attrs) {
        this(context, attrs , 0);
    }

    public TranslationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init() ;
    }


    private void init(){
        paint = new Paint() ;
        paint.setAntiAlias(true);
        bitmap = BitmapFactory.decodeResource(getContext().getResources() , R.drawable.scale) ;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(bitmap , matrix, paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                System.out.print("down");
                matrix.postTranslate(event.getX() , event.getY()) ;
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                matrix.postTranslate(event.getX() , event.getY()) ;
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                System.out.print("up");
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
}
